home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / buffers.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  7KB  |  381 lines

  1. /* --------------------------------- buffers.c ------------------------------ */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* Display list management.
  8. */
  9.  
  10. #include "fly.h"
  11.  
  12.  
  13. #undef gr_1op
  14. extern void FAR FASTCALL
  15. gr_1op (Ushort a)
  16. {
  17.     if (st.buf_avail >= 1 || buffer_new ()) {
  18.         *(st.buf_p)++ = a;
  19.         --st.buf_avail;
  20.     }
  21. }
  22.     
  23. #undef gr_2op
  24. extern void FAR FASTCALL
  25. gr_2op (Ushort a, Ushort b)
  26. {
  27.     if (st.buf_avail >= 2 || buffer_new ()) {
  28.         *(st.buf_p)++ = a;
  29.         *(st.buf_p)++ = b;
  30.         st.buf_avail -= 2;
  31.     }
  32. }
  33.     
  34. #undef gr_4op
  35. extern void FAR FASTCALL
  36. gr_4op (Ushort a, Ushort b, Ushort c, Ushort d)
  37. {
  38.     BUFLINE    *p;
  39.  
  40.     if (st.buf_avail >= 4 || buffer_new ()) {
  41.         p = st.buf_p;
  42.         p[0] = a;
  43.         p[1] = b;
  44.         p[2] = c;
  45.         p[3] = d;
  46.         st.buf_p = p + 4;
  47.         st.buf_avail -= 4;
  48.     }
  49. }
  50.  
  51. LOCAL_FUNC void NEAR FASTCALL
  52. show_buf (BUFFER *b)
  53. {
  54.     BUFLINE        *p;
  55.     BUFLINE        *end;
  56.     Uint        op;
  57.     Uint        x;
  58. #ifdef CHECK_GR
  59.     Uint        minx, maxx, miny, maxy;
  60. #endif
  61.     static Uint    color = 0;
  62.     void        (FAR* MoveTo) (Uint x1, Uint y1);
  63.     void        (FAR* DrawTo) (Uint x2, Uint y2, Uint c);
  64.     void        (FAR* Ellipse) (Uint x, Uint y, Uint rx, Uint ry,
  65.                     Uint c);
  66.  
  67.     MoveTo  = Gr->MoveTo;
  68.     DrawTo  = Gr->DrawTo;
  69.     Ellipse = Gr->Ellipse ? Gr->Ellipse : NoEllipse;
  70.  
  71. #ifdef CHECK_GR
  72.     minx = (Uint)CS->minx;
  73.     maxx = (Uint)CS->minx + CS->sizex - 1;
  74.     miny = (Uint)CS->miny;
  75.     maxy = (Uint)CS->miny + CS->sizey - 1;
  76. #endif
  77.  
  78.     p = b->first;
  79.     end = b->p;
  80.     for (; p < end;) {
  81.         x = *p++;
  82.         op = x & T_MASK;
  83.         x &=    ~T_MASK;
  84.         switch (op) {
  85.         case T_COLOR:
  86. #if 0
  87.             color = st.colors[x];
  88. #else
  89.             color = x;
  90. #endif
  91.             break;
  92.         case T_DRAW:
  93. #ifdef CHECK_GR
  94. if (x  < minx || x  > maxx || p[0] < miny || p[0] > maxy) {
  95.         LogPrintf ("Draw(%d,%d) [%d,%d]\n", x, p[0], maxx, maxy);
  96.         break;
  97. }
  98. #endif
  99.             DrawTo (x, *p++, color);
  100.             ++STATS_DRAWCOUNT;
  101.             break;
  102.         case T_MOVE:
  103. #ifdef CHECK_GR
  104. if (x  < minx || x  > maxx || p[0] < miny || p[0] > maxy) {
  105.         LogPrintf ("Move(%d,%d) [%d,%d]\n", x, p[0], maxx, maxy);
  106.         break;
  107. }
  108. #endif
  109.             MoveTo (x, *p++);
  110.             ++STATS_MOVECOUNT;
  111.             break;
  112.         case T_MODE:
  113.             if (Gr->SetWriteMode)
  114.                 Gr->SetWriteMode (x);
  115.             break;
  116.         case T_ELLIPSE:
  117. #ifdef CHECK_GR
  118. if (x-p[1] < minx || x+p[1]  > maxx || p[0]-p[2] < miny || p[0]+p[2] > maxy) {
  119.         LogPrintf ("Ellipse(%d,%d,%d,%d) [%d,%d]\n",
  120.         x, p[0], p[1], p[2], maxx, maxy);
  121.         break;
  122. }
  123. #endif
  124.             Ellipse (x, p[0], p[1], p[2],
  125.                 color);
  126.             p += 3;
  127.             break;
  128.         case T_NOP:
  129.             break;
  130.         default:        /* should not happen! */
  131.             ++STATS_ERRBUFLINE;
  132.             break;
  133.         }
  134.     }
  135.     return;
  136. }
  137.  
  138. extern void FAR
  139. buffer_show (BUFFER *b)
  140. {
  141.     if (Gr->SetWriteMode)
  142.         Gr->SetWriteMode (T_MSET);        /* initial mode */
  143.  
  144.     for (; b; b = b->next) {
  145.         show_buf (b);
  146.         sys_poll (3);
  147.     }
  148. }
  149.  
  150. extern void FAR
  151. buffers_show (int which)
  152. {
  153.     int    i;
  154.  
  155.     buffer_show (st.bufs[which]);
  156.  
  157.     for (i = 0; i < NHDD; ++i) {
  158.         if (!(st.hdd[i].flags & HDF_ON))
  159.             continue;
  160.         buffer_show (st.hdd[i].bufs[which]);
  161.     }
  162. }
  163.  
  164. LOCAL_FUNC void NEAR FASTCALL
  165. erase_buf (BUFFER *b, Uint color)
  166. {
  167.     BUFLINE        *p;
  168.     BUFLINE        *end;
  169.     Uint        op;
  170.     Uint        x;
  171. #ifdef CHECK_GR
  172.     Uint        minx, maxx, miny, maxy;
  173. #endif
  174.     void        (FAR* MoveTo) (Uint x1, Uint y1);
  175.     void        (FAR* DrawTo) (Uint x2, Uint y2, Uint c);
  176.     void        (FAR* Ellipse) (Uint x, Uint y, Uint rx, Uint ry,
  177.                     Uint c);
  178.  
  179.     color   = st.colors[color];
  180.     MoveTo     = Gr->MoveTo;
  181.     DrawTo     = Gr->DrawTo;
  182.     Ellipse = Gr->Ellipse ? Gr->Ellipse : NoEllipse;
  183.  
  184. #ifdef CHECK_GR
  185.     minx = (Uint)CS->minx;
  186.     maxx = (Uint)CS->minx + CS->sizex - 1;
  187.     miny = (Uint)CS->miny;
  188.     maxy = (Uint)CS->miny + CS->sizey - 1;
  189. #endif
  190.  
  191.     p = b->first;
  192.     end = b->p;
  193.     for (; p < end;) {
  194.         x = *p++;
  195.         op = x & T_MASK;
  196.         x &=    ~T_MASK;
  197.         switch (op) {
  198.         case T_DRAW:
  199. #ifdef CHECK_GR
  200. if (x  < minx || x  > maxx || p[0] < miny || p[0] > maxy) {
  201.         LogPrintf ("Draw(%d,%d) [%d,%d]\n", x, p[0], maxx, maxy);
  202.         break;
  203. }
  204. #endif
  205.             DrawTo (x, *p++, color);
  206.             ++STATS_DRAWCOUNT;
  207.             break;
  208.         case T_MOVE:
  209. #ifdef CHECK_GR
  210. if (x  < minx || x  > maxx || *p < miny || *p > maxy) {
  211.         LogPrintf ("Move(%d,%d) [%d,%d]\n", x, *p, maxx, maxy);
  212.         break;
  213. }
  214. #endif
  215.             MoveTo (x, *p++);
  216.             ++STATS_MOVECOUNT;
  217.             break;
  218.         case T_ELLIPSE:
  219. #ifdef CHECK_GR
  220. if (x-p[1] < minx || x+p[1]  > maxx || p[0]-p[2] < miny || p[0]+p[2] > maxy) {
  221.         LogPrintf ("Ellipse(%d,%d,%d,%d) [%d,%d]\n",
  222.         x, p[0], p[1], p[2], maxx, maxy);
  223.         break;
  224. }
  225. #endif
  226.             Ellipse (x, p[0], p[1], p[2], color);
  227.             p += 3;
  228.             break;
  229.         case T_COLOR:
  230.         case T_MODE:
  231.         case T_NOP:
  232.             break;
  233.         default:        /* should not happen! */
  234.             ++STATS_ERRBUFLINE;
  235.             break;
  236.         }
  237.     }
  238.     return;
  239. }
  240.  
  241. extern void FAR
  242. buffer_erase (BUFFER *b, Uint color)
  243. {
  244.     if (Gr->SetWriteMode)
  245.         Gr->SetWriteMode (T_MSET);        /* initial mode */
  246.  
  247.     for (; b; b = b->next) {
  248.         erase_buf (b, color);
  249.         sys_poll (4);
  250.     }
  251. }
  252.  
  253. extern void FAR
  254. buffers_erase (int which)
  255. {
  256.     int    i;
  257.  
  258.     if (Gr->Clear) {
  259.         Gr->Clear (CS);
  260.         sys_poll (5);
  261.         return;
  262.     }
  263.  
  264.     buffer_erase (st.bufs[which], CS->BgColor);
  265.  
  266.     for (i = 0; i < NHDD; ++i) {
  267.         if (!(st.hdd[i].flags & HDF_ON))
  268.             continue;
  269.         buffer_erase (st.hdd[i].bufs[which], st.hdd[i].BgColor);
  270.     }
  271. }
  272.  
  273. extern void FAR
  274. buffer_free (BUFFER *b)
  275. {
  276.     BUFFER    *next;
  277.  
  278.     for (; b; b = next) {
  279.         next = b->next;
  280.         memory_free (b,
  281.             sizeof (*b) + (BUFLEN - 1) * sizeof (*b->first));
  282.         --st.nbuffers;
  283.     }
  284. }
  285.  
  286. extern void FAR
  287. buffers_free (int which)
  288. {
  289.     int    i;
  290.  
  291.     buffer_free (st.bufs[which]);
  292.     st.bufs[which] = 0;
  293.  
  294.     for (i = 0; i < NHDD; ++i) {
  295.         if (!(st.hdd[i].flags & HDF_ON))
  296.             continue;
  297.         buffer_free (st.hdd[i].bufs[which]);
  298.         st.hdd[i].bufs[which] = 0;
  299.     }
  300. }
  301.  
  302. extern void FAR
  303. buffers_term (void)
  304. {
  305.     int    i;
  306.  
  307.     for (i = 0; i < NBUFS; ++i)
  308.         buffers_free (i);
  309.  
  310.     buffer_free (st.buf[HEAD]);
  311.     st.buf[HEAD] = st.buf[TAIL] = NULL;
  312.     st.buf_p = NULL;
  313. }
  314.  
  315. LOCAL_FUNC long NEAR FASTCALL
  316. buffer_size (BUFFER *b)
  317. {
  318.     long    l;
  319.  
  320.     for (l = 0; b; b = b->next)
  321.         l += b->p - b->first;
  322.     return (l);
  323. }
  324.  
  325. extern long FAR
  326. buffers_size (int which)
  327. {
  328.     int    i;
  329.     long    l;
  330.  
  331.     l = buffer_size (st.bufs[which]);
  332.     for (i = 0; i < NHDD; ++i) {
  333.         if (!(st.hdd[i].flags & HDF_ON))
  334.             continue;
  335.         l += buffer_size (st.hdd[i].bufs[which]);
  336.     }
  337.     return (l);
  338. }
  339.  
  340. extern void FAR
  341. buffer_close (void)
  342. {
  343.     if (st.buf[TAIL])
  344.         st.buf[TAIL]->p = st.buf_p;
  345.     st.buf_p     = NULL;
  346.     st.buf_avail = 0;
  347. }
  348.  
  349. extern BUFFER * FAR
  350. buffer_new (void)
  351. {
  352.     BUFFER    *b;
  353.  
  354.     if (st.buf_p)
  355.         buffer_close ();
  356.  
  357.     if (st.nbuffers >= st.maxbuffers)
  358.         return (NULL);
  359.  
  360.     b = (BUFFER *) memory_alloc (sizeof (*b) +
  361.                     (BUFLEN - 1) * sizeof (*b->first));
  362.     if (!b)
  363.         return (NULL);
  364.  
  365.     ++st.nbuffers;
  366.     b->next = 0;
  367.     b->p = b->first;
  368.     if (st.buf[TAIL])
  369.         st.buf[TAIL]->next = b;
  370.     else
  371.         st.buf[HEAD] = b;
  372.     st.buf[TAIL] = b;
  373.  
  374. /* redundant copies for speed.
  375. */
  376.     st.buf_p     = b->p;
  377.     st.buf_avail = BUFLEN;
  378.  
  379.     return (b);
  380. }
  381.